{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "“概说南海”成功保存。\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "# 发起请求\n",
    "url = 'http://www.thesouthchinasea.org.cn/about.html'\n",
    "response = requests.get(url)\n",
    "\n",
    "# 解析HTML内容\n",
    "soup = BeautifulSoup(response.content, 'html.parser')\n",
    "\n",
    "# 查找包含标题为“概说南海”的元素\n",
    "section = soup.find('h3', text='概说南海')\n",
    "\n",
    "if section:\n",
    "    # 获取概说南海栏目标题和内容\n",
    "    content = f\"{section.get_text(strip=True)}\\n\\n\"\n",
    "    next_element = section.find_next_sibling()\n",
    "    while next_element and next_element.name != \"h3\":\n",
    "        if next_element.name == \"p\":  # 只获取段落内容\n",
    "            paragraph_text = next_element.get_text(strip=True)\n",
    "            paragraph_text = paragraph_text.replace(\"[更多]\", \"\")\n",
    "            content += paragraph_text + \"\\n\"\n",
    "        next_element = next_element.find_next()\n",
    "    \n",
    "    # 将内容存储到文件中\n",
    "    with open('概说南海.txt', 'w', encoding='utf-8') as file:\n",
    "        file.write(content)\n",
    "    print('“概说南海”成功保存。')\n",
    "else:\n",
    "    print('未找到“概说南海”栏目的内容。')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "天气信息已成功保存。\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "# 目标网页的URL\n",
    "url = \"http://www.tianqihoubao.com/lishi/beijing/month/201909.html\"\n",
    "response = requests.get(url)\n",
    "soup = BeautifulSoup(response.text, \"html.parser\")\n",
    "weather_table = soup.find(\"table\", class_=\"b\")\n",
    "rows = weather_table.find_all(\"tr\")[1:]\n",
    "\n",
    "# 创建文件\n",
    "with open(\"北京天气201909.csv\", mode=\"w\", encoding=\"utf-8\") as file:\n",
    "    file.write(\"日期,温度,天气情况\\n\")\n",
    "    for row in rows:\n",
    "        columns = row.find_all(\"td\")\n",
    "        date = columns[0].text.strip()\n",
    "        temperature = ' '.join(columns[2].text.strip().split())\n",
    "        weather = ' '.join(columns[1].text.strip().split())\n",
    "        \n",
    "        # 将提取的数据写入CSV文件的一行中\n",
    "        file.write(f\"{date},{temperature},{weather}\\n\")\n",
    "\n",
    "print(\"天气信息已成功保存。\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
